var url = serverPre + "/manager/yzt/v1/company"

var bsConf = {

    queryFormId: "company-query-form",

    createFormId: "company-create-form",

    updateFormId: "company-update-form",

    tableID: "id",

    queryCols: 3,

    queryFloat: "right",

    voName: "companyInfo",
    initData: true,
    queryConfigs:                   //页面头部查询form
        [
            {
                type: 'text',
                label: '客户号',
                name: 'customerNo',
                default: '',
                verify: ''
            },
            {
                type: 'select',
                label: '客户类型',
                name: 'customerType',
                default: '',
                verify: '',
                cols: [[2, '企业客户'], [3, '金融机构']]
            },
            {
                type: 'text',
                label: '客户名称',
                name: 'customerName',
                default: '',
                verify: ''
            },
            {
                type: 'datetime',
                label: '创建时间',
                name: 'createTimeStart',
                default: '',
                verify: ''
            },
            {
                type: 'datetime',
                label: '至',
                name: 'createTimeEnd',
                default: '',
                verify: ''
            },
            {
                type: 'text',
                label: '信用代码',
                name: 'unifiedCode',
                default: '',
                verify: ''
            },
            // {
            //     type: 'select',
            //     label: '证件类型',
            //     name: 'idCardType',
            //     default: '',
            //     verify: '',
            //     cols: [[1, '身份证'], [2, '护照'], [3, '港澳台通行证'], [4, '外国人永久居留证'], [5, '军官证']]
            // },
            {
                type: 'text',
                label: '机构代码',
                name: 'orgNo',
                default: '',
                verify: ''
            },
            // {
            //     type: 'datetime',
            //     label: '更新时间',
            //     name: 'updateTimeStart',
            //     default: '',
            //     verify: ''
            // },
            // {
            //     type: 'datetime',
            //     label: '至',
            //     name: 'updateTimeEnd',
            //     default: '',
            //     verify: ''
            // },
            {
                type: 'select',
                label: '来源系统',
                name: 'systemSign',
                dataUrl: serverPre + "/manager/yzt/v1/system/dictInfo?accessed=1",
                nameProperty: 'systemName',
                idProperty: 'systemSign'
            },
        ],

    //列表内容
    tableCols: [
        {type: 'checkbox', width: 50}
        , {field: 'id', title: '序号', type: 'numbers'}
        , {field: 'customerNo', width: 180, title: '客户号'}
        , {field: 'customerType', width: 100, title: '客户类型'}
        , {field: 'customerName', width: 140, title: '客户名称'}
        , {field: 'unifiedCode', width: 180, title: '统一社会信用码'}
        , {field: 'orgNo', width: 150, title: '组织机构代码'}
        , {field: 'licenseNo', width: 150, title: '营业执照编号'}
        , {field: 'taxNo', width: 150, title: '税务登记号'}
        , {field: 'legalName', width: 100, title: '法人姓名'}
        , {field: 'idCardType', width: 130, title: '法人证件类型'}
        , {field: 'idCardNo', width: 180, title: '法人证件号码'}
        // , {field: 'elementsVerified', width: 150, title: '信息核查情况'}
        // , {field: 'elementsTime', width: 180, title: '信息核查时间'}
        , {field: 'createTime', width: 180, title: '创建时间'}
        , {field: 'updateTime', width: 180, title: '更新时间'}
        , {field: 'systemName',width: 200, title: '来源系统'}
        , {
            fixed: 'right', width: 100, title: '操作', templet: function (d) {
                return "" +
                    "  <button dataid='" + d.id + "' onclick=\"showCompanyDetail('" + d.customerNo + "');\" class=\"layui-btn layui-btn-sm bs-button-update\">显示详情</button>\n" +
                    "";
            }
        }
    ],

    //templet: function (d) {return d.sts == '1' ? '启用' : '禁止'}
    //以下是各种回调
    //新建之后的回调
    // createCallback: function (result) {
    //     alert("createCallback:" + result.code);
    // },
    // //修改之后的回调  只针对默认实现
    // updateCallback: function (result) {
    //     alert("updateCallback:" + result.code);
    // },
    // //删除之后的回调  只针对默认实现
    // deleteCallback: function (result) {
    //     alert("deleteCallback:" + result.code);
    // },
    // //批量删除之后的回调
    // batchDeleteCallback: function (result) {
    //     alert("batchDeleteCallback:" + result.code);
    // },
    // //查询之后的回调   这里是默认操作之后的补充操作
    // queryCallback: function (result) {
    //     alert("queryCallback:" + result.code);
    // },

}
var pied = false;

//显示用户详情
function showCompanyDetail(id) {
    $.ajax({
        type: 'get',
        url: serverPre + "/manager/yzt/v1/company/extend?customerNo=" + id,
        dataType: 'json',
        headers: {"token": getCookie("token")},
        async: false,
        cache: false,
        success: function (result) {

            // var tablec = "<table class=\"layui-table\">\n" +
            //     "  <colgroup>\n" +
            //     "    <col width=\"40\">\n" +
            //     "    <col width=\"40\">\n" +
            //     "    <col width=\"60\">\n" +
            //     "    <col width=\"60\">\n" +
            //     "    <col width=\"60\">\n" +
            //     "    <col width=\"60\">\n" +
            //     "    <col>\n" +
            //     "  </colgroup>\n" +
            //     "  <thead>\n" +
            //     "    <tr>\n" +
            //     "      <th>客户角色</th>\n" +
            //     "      <th>系统标识</th>\n" +
            //     "      <th>子系统标识</th>\n" +
            //     "      <th>角色状态</th>\n" +
            //     "      <th>创建时间</th>\n" +
            //     "      <th>更新时间</th>\n" +
            //     "    </tr> \n" +
            //     "  </thead>\n" +
            //     "  <tbody>\n";
            // if (result.data.customerRoles.length > 0) {
            //     for (var i = 0; i < result.data.customerRoles.length; i++) {
            //         var v = result.data.customerRoles[i];
            //         tablec += "<tr>";
            //         tablec += "<td>";
            //         tablec += v['customerRole'] == null ? "" : v['customerRole'];
            //         tablec += "</td>";
            //
            //         tablec += "<td>";
            //         tablec += v['systemSign'] == null ? "" : v['systemSign'];
            //         tablec += "</td>";
            //
            //         tablec += "<td>";
            //         tablec += v['subSystemSign'] == null ? "" : v['subSystemSign'];
            //         tablec += "</td>";
            //
            //         tablec += "<td>";
            //         tablec += v['status'] == null ? "" : v['status'];
            //         tablec += "</td>";
            //
            //         tablec += "<td>";
            //         tablec += v['createTime'] == null ? "" : v['createTime'];
            //         tablec += "</td>";
            //
            //         tablec += "<td>";
            //         tablec += v['updateTime'] == null ? "" : v['updateTime'];
            //         tablec += "</td>";
            //
            //         tablec += "</tr>";
            //     }
            // } else {
            //     tablec += "<tr><td colspan='6' style='text-align: center'>无数据</td></tr> "
            // }
            // tablec += "</tbody></table>";

            /* var tablec2 = "<table class=\"layui-table\">\n" +
                 "  <colgroup>\n" +
                 "    <col width=\"40\">\n" +
                 "    <col width=\"40\">\n" +
                 "    <col width=\"40\">\n" +
                 "    <col width=\"40\">\n" +
                 "    <col width=\"40\">\n" +
                 "    <col width=\"40\">\n" +
                 "    <col width=\"40\">\n" +
                 "    <col>\n" +
                 "  </colgroup>\n" +
                 "  <thead>\n" +
                 "    <tr>\n" +
                 "      <th>客户姓名</th>\n" +
                 "      <th>证件类型</th>\n" +
                 "      <th>证件号码</th>\n" +
                 "      <th>系统标识</th>\n" +
                 "      <th>子系统标识</th>\n" +
                 "      <th>创建时间</th>\n" +
                 "      <th>更新时间</th>\n" +
                 "    </tr> \n" +
                 "  </thead>\n" +
                 "  <tbody>\n" ;
             for (var i = 0; i < result.data.idInfos.length;  i ++) {
                 var v = result.data.idInfos[i];
                 tablec2 += "<tr><td>" + v['customerName'] + "</td><td>"+ v['idCardType'] + "</td><td>"+ v['idCardNo'] + "</td><td>" + v['subSystemSign'] + "</td><td>"+ v['createTime'] + "</td><td>" + v['updateTime'] + "</td></tr>\n";
             }
             tablec2 += "</tbody></table>";*/
            var tablec3 = "<table class=\"layui-table\">\n" +
                "  <colgroup>\n" +
                "    <col width=\"40\">\n" +
                "    <col width=\"40\">\n" +
                "    <col width=\"40\">\n" +
                "    <col width=\"40\">\n" +
                "    <col width=\"40\">\n" +
                "    <col>\n" +
                "  </colgroup>\n" +
                "  <thead>\n" +
                "    <tr>\n" +
                "      <th>子系统标识</th>\n" +
                "      <th>联系类型</th>\n" +
                "      <th>联系方式</th>\n" +
                "      <th>创建时间</th>\n" +
                "      <th>更新时间</th>\n" +
                "    </tr> \n" +
                "  </thead>\n" +
                "  <tbody>\n";
            if (result.data.contacts.length > 0) {
                for (var i = 0; i < result.data.contacts.length; i++) {
                    var v = result.data.contacts[i];
                    tablec3 += "<tr><td>" + (v['subSystemSign'] == null ? "" : v['subSystemSign']) + "</td><td>" + (v['contactType'] == null ? "" : v['contactType']) + "</td><td>" + (v['contactInfo'] == null ? "" : v['contactInfo']) + "</td><td>" + (v['createTime'] == null ? "" : v['createTime']) + "</td><td>" + (v['updateTime'] == null ? "" : v['updateTime']) + "</td></tr>\n";
                }
            } else {
                tablec3 += "<tr><td colspan='5' style='text-align: center'>无数据</td></tr> "
            }
            tablec3 += "</tbody></table>";
            var tablec4 = "<table class=\"layui-table\">\n" +
                "  <colgroup>\n" +
                "    <col width=\"40\">\n" +
                "    <col width=\"40\">\n" +
                "    <col width=\"40\">\n" +
                "    <col width=\"40\">\n" +
                "    <col>\n" +
                "  </colgroup>\n" +
                "  <thead>\n" +
                "    <tr>\n" +
                "      <th>子系统标识</th>\n" +
                "      <th>来源类型</th>\n" +
                "      <th>来源用户号</th>\n" +
                "      <th>转化时间</th>\n" +
                "    </tr> \n" +
                "  </thead>\n" +
                "  <tbody>\n";
            if (result.data.preChanges.length > 0) {
                for (var i = 0; i < result.data.preChanges.length; i++) {
                    var v = result.data.preChanges[i];

                    tablec4 += "<tr><td>" + (v['subSystemSign'] == null ? "" : v['subSystemSign']) + "</td><td>" + (v['preType'] == null ? "" : v['preType']) + "</td><td>" + (v['preCustomerNo'] == null ? "" : v['preCustomerNo']) + "</td><td>" + (v['updateTime'] == null ? "" : v['updateTime']) + "</td></tr>\n";
                }
            } else {
                tablec4 += "<tr><td colspan='4' style='text-align: center'>无数据</td></tr> "
            }
            tablec4 += "</tbody></table>";
            var content = "<div style='margin: 10px;'>客户号：" + id +
                // "<blockquote class=\"layui-elem-quote\">客户角色：</blockquote>" + tablec + "<br/><br/>" +
                /* "<blockquote class=\"layui-elem-quote\">证件信息：</blockquote><br/>" + tablec2 +*/
                "<blockquote class=\"layui-elem-quote\">联系信息：</blockquote>" + tablec3 + "<br/><br/>" +
                "<blockquote class=\"layui-elem-quote\">转化来源：</blockquote>" + tablec4 + "<br/><br/>" +
                "</div>";
            layer.open({
                type: 1,
                title: '对公客户详情信息',
                shadeClose: true,
                shade: false,
                maxmin: true,
                content: content,
                area: ['80%', '70%'],
                success: function () {

                }
            });
        }
    });

}

function updateIdInfo(obj) {
    if (obj.className.indexOf("layui-btn-disabled") < 0) {
        var checkStatus = table.checkStatus('id'),
            data = checkStatus.data;
        if (data.length === 0) {
            layer.msg('请选择一条数据处理');
            return;
        } else if (data.length > 1) {
            layer.msg('只允许处理一条数据');
            return;
        }
        var v = data[0];
        var tablec = "<div id='updateCompany' style='margin-top: 30px;margin-left: 50px;font-size: 14px;line-height:25px'><table align='center' style='margin-top: 5px;'>";
        tablec += "<tr>原客户姓名:   " + v['legalName'] + "</tr><br/>";
        tablec += "<tr>原证件类型:   " + v['idCardType'] + "</tr><br/>";
        tablec += "<tr>原证件号码:   " + v['idCardNo'] + "</tr><br/><br/>";
        tablec += "<tr>新客户姓名:   " + "<input type='text' id='newLegalName' style='width:150px;'>" + "</tr><br/>";
        tablec += "<tr>新证件类型:   " + "<select id='newIdCardType' style='width:154px;' name='newIdCardType'><option value ='1'>身份证</option><option value ='2'>护照</option><option value ='3'>港澳台通行证</option><option value ='4'>外国人永久居留证</option><option value ='5'>军官证</option></select>" + "</tr><br/>";
        tablec += "<tr>新证件号码:   " + "<input type='text' id='newIdCardNo' style='width:150px;'>" + "</tr>";
        tablec += "</table></div>";
        var content = tablec;
        var index = layer.open({
            type: 1,
            skin: 'layui-layer-molv',//样式类名
            title: '法人证件信息修改',
            shadeClose: true,
            shade: false,
            maxmin: true,
            content: content,
            area: ['350px', '350px'],
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                var newLegalName = $("#newLegalName").val();
                var newIdCardType = $("#newIdCardType").val();
                var newIdCardNo = $("#newIdCardNo").val();
                if (newLegalName != "" && newIdCardType != "" && newIdCardNo != "") {
                    updatePre(v['id'], v['customerNo'], newLegalName, newIdCardType, newIdCardNo);
                }
                else {
                    layer.msg("证件信息不能为空", {
                        time: 2000
                    });
                }
                layer.close(index);
            },
            cancel: function (index, layero) {
                layer.close(index);
            }
        });
    }
}

function updatePre(id, customerNo, legalName, idCardType, idCardNo) {
    $.ajax({
        type: 'get',
        url: serverPre + "/manager/yzt/v1/company/update?id=" + id + "&&legalName=" + legalName + "&&idCardType=" + idCardType + "&&idCardNo=" + idCardNo,
        dataType: 'json',
        headers: {"token": getCookie("token")},
        async: false,
        cache: false,
        success: function (result) {
            if (result.code == 0 && result.data.status == 0) {
                var noticeContent = result.data.content;
                var content1 = "<div  style='margin-top: 70px;margin-left: 30px;margin-right:30px;font-size: 14px;line-height:25px' ><span style='font-size: 14px;'>" + "原证件信息正在使用的系统为：" + noticeContent + "   ,请确定是否要修改该客户证件信息？修改完成后将通知以上关联系统进行同步" + "</span></div>";
                var index = layer.open({
                    type: 1,
                    title: '法人证件信息修改',
                    skin: 'layui-layer-molv',//样式类名
                    shadeClose: true,
                    shade: false,
                    maxmin: true,
                    content: content1,
                    area: ['350px', '350px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        updateAndNotice(id, customerNo, legalName, idCardType, idCardNo);
                        queryData(url, pageObj.curr, pageObj.limit);
                        layer.close(index);
                    },
                    cancel: function (index, layero) {
                        layer.close(index);
                    }
                });
            } else if (result.code == 0 && result.data.status == 1) {
                var noticeContent = result.data.content;
                var content2 = "<div  style='margin-top: 70px;margin-left: 30px;margin-right:30px;font-size: 14px;line-height:25px' ><span style='font-size: 14px;'>" + "要修改的客户信息已存在，客户号：" + noticeContent + "   ,不能进行证件信息修改" + "</span></div>";
                var index = layer.open({
                    type: 1,
                    title: '法人证件信息修改',
                    skin: 'layui-layer-molv',//样式类名
                    shadeClose: true,
                    shade: false,
                    maxmin: true,
                    content: content2,
                    area: ['350px', '350px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        layer.close(index);
                    },
                    cancel: function (index, layero) {
                        layer.close(index);
                    }
                });
            } else {
                layer.msg(result.msg, {
                    time: 2000
                });
            }
        }
    });
}

function updateAndNotice(id, customerNo, legalName, idCardType, idCardNo) {
    $.ajax({
        type: 'get',
        url: serverPre + "/manager/yzt/v1/company/notice?id=" + id + "&&customerNo=" + customerNo + "&&legalName=" + legalName + "&&idCardType=" + idCardType + "&&idCardNo=" + idCardNo,
        dataType: 'json',
        headers: {"token": getCookie("token")},
        async: false,
        cache: false,
        success: function (result) {
            if (result.code == 0 && result.data.status == 0) {
                layer.msg("处理成功", {
                    icon: 1,
                    time: 1000
                });
            } else {
                layer.msg("证件修改失败", {
                    time: 2000
                });
            }
        }
    });

}
function company_search() {
    $("#company-query-form").toggle()
}